/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.example.netcracker.ui;

import ru.example.netcracker.ui.listeners.OnClickTrayListener;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Nikolay
 */
public class SysTrayMenu extends PopupMenu implements ActionListener {

    private static final String MENU_OPEN_CMD = "open";
    private static final String MENU_ADD_TASK_CMD = "add";
    private static final String MENU_PREFERENCES_CMD = "pref";
    private static final String MENU_ABOUT_CMD = "about";
    private static final String MENU_EXIT_CMD = "exit";

    private OnClickTrayListener listener;

    public SysTrayMenu() {
        MenuItem openItem = new MenuItem("Открыть");
        openItem.addActionListener(this);
        openItem.setActionCommand(MENU_OPEN_CMD);

        MenuItem addItem = new MenuItem("Добавить задачу");
        addItem.addActionListener(this);
        addItem.setActionCommand(MENU_ADD_TASK_CMD);

        MenuItem prefItem = new MenuItem("Настройки");
        prefItem.addActionListener(this);
        prefItem.setActionCommand(MENU_PREFERENCES_CMD);

        MenuItem aboutItem = new MenuItem("О программе");
        aboutItem.addActionListener(this);
        aboutItem.setActionCommand(MENU_ABOUT_CMD);

        MenuItem exitItem = new MenuItem("Выход");
        exitItem.addActionListener(this);
        exitItem.setActionCommand(MENU_EXIT_CMD);

        add(openItem);
        addSeparator();
        add(addItem);
        add(prefItem);
        addSeparator();
        add(aboutItem);
        addSeparator();
        add(exitItem);
    }

    public void addOnClickTrayListener(OnClickTrayListener listener) {
        this.listener = listener;
    }

    public void removeOnClickTrayListener() {
        this.listener = null;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()) {
            case MENU_OPEN_CMD:
                if (listener != null) {
                    listener.onClickOpen();
                }
                break;
            case MENU_ADD_TASK_CMD:
                if (listener != null) {
                    listener.onClickAdd();
                }
                break;
            case MENU_PREFERENCES_CMD:
                if (listener != null) {
                    listener.onClickPreferences();
                }
                break;
            case MENU_ABOUT_CMD:
                if (listener != null) {
                    listener.onClickAbout();
                }
                break;
            case MENU_EXIT_CMD:
                if (listener != null) {
                    listener.onClickExit();
                }
        }
    }
}
